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;
120 rand_add(r, tv, sizeof(*tv), de <= dde ? de : dde);
122 BURN(tv); x = d = dd = de = dde = 0;
126 /* --- @noise_timer@ --- *
128 * Arguments: @rand_pool *r@ = pointer to a randomness pool
130 * Returns: Nonzero if some randomness was contributed.
132 * Use: Contributes the current time to the randomness pool.
133 * A guess at the number of useful bits contributed is made,
134 * based on first and second order bit differences. This isn't
135 * ever-so reliable, but it's better than nothing.
138 int noise_timer(rand_pool *r)
141 gettimeofday(&tv, 0);
142 return (timer(r, &tv));
145 /* --- @noise_devrandom@ --- *
147 * Arguments: @rand_pool *r@ = pointer to a randomness pool
149 * Returns: Nonzero if some randomness was contributed.
151 * Use: Attempts to obtain some randomness from the system entropy
152 * pool. All bits from the device are assumed to be good.
155 int noise_devrandom(rand_pool *r)
158 octet buf[RAND_POOLSZ];
163 /* --- Be nice to other clients of the random device --- *
165 * Attempt to open the device nonblockingly. If that works, take up to
166 * one bufferful and then close again. If there's no data to be read,
167 * then that's tough and we go away again, on the grounds that the device
168 * needs to get some more entropy from somewhere.
171 if ((fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
172 (fd = open("/dev/arandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
173 (fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) >= 0) {
174 while (n < sizeof(buf)) {
175 if ((len = read(fd, buf + n, sizeof(buf) - n)) <= 0) break;
178 rand_add(r, buf, n, n * 8);
180 if (n == sizeof(buf)) ret = 1;
187 /* --- @noise_setid@ --- *
189 * Arguments: @uid_t uid@ = uid to set
190 * @gid_t gid@ = gid to set
194 * Use: Sets the user and group ids to be used by @noise_filter@
195 * when running child processes. This is useful to avoid
196 * giving shell commands (even carefully written ones) undue
197 * privileges. This interface is Unix-specific
200 void noise_setid(uid_t uid, gid_t gid)
206 /* --- @noise_filter@ --- *
208 * Arguments: @rand_pool *r@ = pointer to a randomness pool
209 * @int good@ = number of good bits per 1024 bits
210 * @const char *c@ = shell command to run
212 * Returns: Nonzero if some randomness was contributed.
214 * Use: Attempts to execute a shell command, and dump it into the
215 * randomness pool. A very rough estimate of the number of
216 * good bits is made, based on the size of the command's output.
217 * This function calls @waitpid@, so be careful. Before execing
218 * the command, the process uid and gid are set to the values
219 * given to @noise_setid@, and an attempt is made to reset the
220 * list of supplementary groups. The environment passed to
221 * the command has been severly lobotimized. If the command
222 * fails to complete within a short time period, it is killed.
223 * Paranoid use of close-on-exec flags for file descriptors is
226 * This interface is Unix-specific.
229 int noise_filter(rand_pool *r, int good, const char *c)
236 const char *env[] = {
237 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
241 /* --- Remember when this business started --- */
243 gettimeofday(&dead, 0);
246 /* --- Create a pipe --- */
251 /* --- Fork a child off --- */
261 /* --- Handle the child end of the deal --- */
266 /* --- Set the pipe as standard output, close standard input --- */
268 close(0); close(1); close(2);
271 if (dup2(fd[1], 1) < 0) _exit(127);
275 if ((f = open("/dev/null", O_RDONLY)) != 0 ||
276 (f = open("/dev/null", O_WRONLY)) != 2)
279 /* --- Play games with uids --- */
281 if (noise_gid != NOISE_NOSETGID) {
284 #ifdef HAVE_SETGROUPS
285 setgroups(1, &noise_gid);
289 if (noise_uid != NOISE_NOSETUID) {
294 /* --- Start the process up --- */
296 execle("/bin/sh", "-c", c, (char *)0, env);
300 /* --- Sort out my end of the deal --- */
304 /* --- Decide on the deadline --- */
306 TV_ADDL(&dead, &dead, 0, NOISE_KIDLIFE);
308 /* --- Now read, and think --- */
311 struct timeval now, diff;
314 gettimeofday(&now, 0);
316 if (TV_CMP(&now, >, &dead))
318 TV_SUB(&diff, &dead, &now);
323 if (select(fd[0] + 1, &rd, 0, 0, &diff) < 0)
325 if (FD_ISSET(fd[0], &rd)) {
329 if ((sz = read(fd[0], buf, sizeof(buf))) <= 0)
331 goodbits = (sz * good) / 128;
332 rand_add(r, buf, sz, goodbits);
337 /* --- We've finished with it: kill the child process --- *
339 * This is morally questionable. On the other hand, I don't want to be
340 * held up in the @waitpid@ call if I can possibly help it. Maybe a
341 * double-fork is worth doing here.
352 /* --- @noise_freewheel@ --- *
354 * Arguments: @rand_pool *r@ = pointer to a randomness pool
356 * Returns: Nonzero if some randomness was contributed.
358 * Use: Runs a free counter for a short while as a desparate attempt
359 * to get randomness from somewhere. This is actually quite
365 static jmp_buf fwjmp;
367 static void fwalarm(int sig)
369 siglongjmp(fwjmp, 1);
372 int noise_freewheel(rand_pool *r)
374 void (*sigal)(int) = 0;
375 struct itimerval oitv, itv = { { 0, 0 }, { 0, 5000 } };
377 volatile uint32 fwcount = 0;
379 if (!sigsetjmp(fwjmp, 1)) {
380 if ((sigal = signal(SIGALRM, fwalarm)) == SIG_ERR)
382 if (setitimer(ITIMER_REAL, &itv, &oitv))
388 STORE32(buf, fwcount);
389 rand_add(r, buf, sizeof(buf), 16);
394 signal(SIGALRM, sigal);
395 if (oitv.it_value.tv_sec || oitv.it_value.tv_usec)
396 TV_SUB(&oitv.it_value, &oitv.it_value, &itv.it_value);
397 setitimer(ITIMER_REAL, &oitv, 0);
403 int noise_freewheel(rand_pool *r)
410 /* --- @noise_enquire@ --- *
412 * Arguments: @rand_pool *r@ = pointer to a randomness pool
414 * Returns: Nonzero if some randomness was contributed.
416 * Use: Runs some shell commands to enquire about the prevailing
417 * environment. This can gather quite a lot of low-quality
421 int noise_enquire(rand_pool *r)
427 { "ps alxww || ps -elf", 16 },
429 { "ifconfig -a", 8 },
432 { "ls -align /tmp/.", 10 },
437 for (i = 0; tab[i].cmd; i++)
438 noise_filter(r, tab[i].rate, tab[i].cmd);
442 /* --- @noise_acquire@ --- *
444 * Arguments: @rand_pool *r@ = pointer to a randomness pool
448 * Use: Acquires some randomness from somewhere.
451 void noise_acquire(rand_pool *r)
454 for (i = 0; i < 8; i++)
456 if (!noise_devrandom(r)) {
458 for (i = 0; i < 8; i++)
463 /*----- That's all, folks -------------------------------------------------*/