3 * Acquisition of environmental noise (Unix-specific)
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
48 #include <mLib/bits.h>
55 /*----- Magical numbers ---------------------------------------------------*/
57 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
58 #define MILLION 1000000 /* One million */
60 /*----- Noise source definition -------------------------------------------*/
62 const rand_source noise_source = { noise_acquire, noise_timer };
64 /*----- Static variables --------------------------------------------------*/
66 /* --- Timer differences --- */
68 static unsigned long noise_last; /* Last time recorded */
69 static unsigned long noise_diff; /* Last first order difference */
71 /* --- Setuid program handling --- */
73 static uid_t noise_uid = NOISE_NOSETUID; /* Uid to set to spawn processes */
74 static gid_t noise_gid = NOISE_NOSETGID; /* Gid to set to spawn processes */
76 /*----- Main code ---------------------------------------------------------*/
78 /* --- @bitcount@ --- *
80 * Arguments: @unsigned long x@ = a word containing bits
82 * Returns: The number of bits set in the word.
85 static int bitcount(unsigned long x)
87 char ctab[] = { 0, 1, 1, 2, 1, 2, 2, 3,
88 1, 2, 2, 3, 2, 3, 3, 4 };
91 count += ctab[x & 0xfu];
99 * Arguments: @rand_pool *r@ = pointer to randomness pool
100 * @struct timeval *tv@ = pointer to time block
102 * Returns: Nonzero if some randomness was contributed.
104 * Use: Low-level timer contributor.
107 static int timer(rand_pool *r, struct timeval *tv)
109 unsigned long x, d, dd;
113 x = tv->tv_usec + MILLION * tv->tv_sec;
119 rand_add(r, tv, sizeof(*tv), de <= dde ? de : dde);
121 BURN(tv); x = d = dd = de = dde = 0;
125 /* --- @noise_timer@ --- *
127 * Arguments: @rand_pool *r@ = pointer to a randomness pool
129 * Returns: Nonzero if some randomness was contributed.
131 * Use: Contributes the current time to the randomness pool.
132 * A guess at the number of useful bits contributed is made,
133 * based on first and second order bit differences. This isn't
134 * ever-so reliable, but it's better than nothing.
137 int noise_timer(rand_pool *r)
140 gettimeofday(&tv, 0);
141 return (timer(r, &tv));
144 /* --- @noise_devrandom@ --- *
146 * Arguments: @rand_pool *r@ = pointer to a randomness pool
148 * Returns: Nonzero if some randomness was contributed.
150 * Use: Attempts to obtain some randomness from the system entropy
151 * pool. All bits from the device are assumed to be good.
154 int noise_devrandom(rand_pool *r)
157 octet buf[RAND_POOLSZ];
162 /* --- Be nice to other clients of the random device --- *
164 * Attempt to open the device nonblockingly. If that works, take up to
165 * one bufferful and then close again. If there's no data to be read,
166 * then that's tough and we go away again, on the grounds that the device
167 * needs to get some more entropy from somewhere.
170 if ((fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
171 (fd = open("/dev/arandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
172 (fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) >= 0) {
173 while (n < sizeof(buf)) {
174 if ((len = read(fd, buf + n, sizeof(buf) - n)) <= 0) break;
177 rand_add(r, buf, n, n * 8);
179 if (n == sizeof(buf)) ret = 1;
186 /* --- @noise_setid@ --- *
188 * Arguments: @uid_t uid@ = uid to set
189 * @gid_t gid@ = gid to set
193 * Use: Sets the user and group ids to be used by @noise_filter@
194 * when running child processes. This is useful to avoid
195 * giving shell commands (even carefully written ones) undue
196 * privileges. This interface is Unix-specific
199 void noise_setid(uid_t uid, gid_t gid)
205 /* --- @noise_filter@ --- *
207 * Arguments: @rand_pool *r@ = pointer to a randomness pool
208 * @int good@ = number of good bits per 1024 bits
209 * @const char *c@ = shell command to run
211 * Returns: Nonzero if some randomness was contributed.
213 * Use: Attempts to execute a shell command, and dump it into the
214 * randomness pool. A very rough estimate of the number of
215 * good bits is made, based on the size of the command's output.
216 * This function calls @waitpid@, so be careful. Before execing
217 * the command, the process uid and gid are set to the values
218 * given to @noise_setid@, and an attempt is made to reset the
219 * list of supplementary groups. The environment passed to
220 * the command has been severly lobotimized. If the command
221 * fails to complete within a short time period, it is killed.
222 * Paranoid use of close-on-exec flags for file descriptors is
225 * This interface is Unix-specific.
228 int noise_filter(rand_pool *r, int good, const char *c)
235 const char *env[] = {
236 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
240 /* --- Remember when this business started --- */
242 gettimeofday(&dead, 0);
245 /* --- Create a pipe --- */
250 /* --- Fork a child off --- */
260 /* --- Handle the child end of the deal --- */
265 /* --- Set the pipe as standard output, close standard input --- */
267 close(0); close(1); close(2);
270 if (dup2(fd[1], 1) < 0) _exit(127);
274 if ((f = open("/dev/null", O_RDONLY)) != 0 ||
275 (f = open("/dev/null", O_WRONLY)) != 2)
278 /* --- Play games with uids --- */
280 if (noise_gid != NOISE_NOSETGID) {
283 #ifdef HAVE_SETGROUPS
284 setgroups(1, &noise_gid);
288 if (noise_uid != NOISE_NOSETUID) {
293 /* --- Start the process up --- */
295 execle("/bin/sh", "-c", c, (char *)0, env);
299 /* --- Sort out my end of the deal --- */
303 /* --- Decide on the deadline --- */
305 TV_ADDL(&dead, &dead, 0, NOISE_KIDLIFE);
307 /* --- Now read, and think --- */
310 struct timeval now, diff;
313 gettimeofday(&now, 0);
315 if (TV_CMP(&now, >, &dead))
317 TV_SUB(&diff, &dead, &now);
322 if (select(fd[0] + 1, &rd, 0, 0, &diff) < 0)
324 if (FD_ISSET(fd[0], &rd)) {
328 if ((sz = read(fd[0], buf, sizeof(buf))) <= 0)
330 goodbits = (sz * good) / 128;
331 rand_add(r, buf, sz, goodbits);
336 /* --- We've finished with it: kill the child process --- *
338 * This is morally questionable. On the other hand, I don't want to be
339 * held up in the @waitpid@ call if I can possibly help it. Maybe a
340 * double-fork is worth doing here.
351 /* --- @noise_freewheel@ --- *
353 * Arguments: @rand_pool *r@ = pointer to a randomness pool
355 * Returns: Nonzero if some randomness was contributed.
357 * Use: Runs a free counter for a short while as a desparate attempt
358 * to get randomness from somewhere. This is actually quite
364 static jmp_buf fwjmp;
366 static void fwalarm(int sig)
368 siglongjmp(fwjmp, 1);
371 int noise_freewheel(rand_pool *r)
373 void (*sigal)(int) = 0;
374 struct itimerval oitv, itv = { { 0, 0 }, { 0, 5000 } };
376 volatile uint32 fwcount = 0;
378 if (!sigsetjmp(fwjmp, 1)) {
379 if ((sigal = signal(SIGALRM, fwalarm)) == SIG_ERR)
381 if (setitimer(ITIMER_REAL, &itv, &oitv))
387 STORE32(buf, fwcount);
388 rand_add(r, buf, sizeof(buf), 16);
393 signal(SIGALRM, sigal);
394 if (oitv.it_value.tv_sec || oitv.it_value.tv_usec)
395 TV_SUB(&oitv.it_value, &oitv.it_value, &itv.it_value);
396 setitimer(ITIMER_REAL, &oitv, 0);
402 int noise_freewheel(rand_pool *r)
409 /* --- @noise_enquire@ --- *
411 * Arguments: @rand_pool *r@ = pointer to a randomness pool
413 * Returns: Nonzero if some randomness was contributed.
415 * Use: Runs some shell commands to enquire about the prevailing
416 * environment. This can gather quite a lot of low-quality
420 int noise_enquire(rand_pool *r)
426 { "ps alxww || ps -elf", 16 },
428 { "ifconfig -a", 8 },
431 { "ls -align /tmp/.", 10 },
436 for (i = 0; tab[i].cmd; i++)
437 noise_filter(r, tab[i].rate, tab[i].cmd);
441 /* --- @noise_acquire@ --- *
443 * Arguments: @rand_pool *r@ = pointer to a randomness pool
447 * Use: Acquires some randomness from somewhere.
450 void noise_acquire(rand_pool *r)
453 for (i = 0; i < 8; i++)
455 if (!noise_devrandom(r)) {
457 for (i = 0; i < 8; i++)
462 /*----- That's all, folks -------------------------------------------------*/